package com.cn.dream_order_server.aop;

import com.cn.dream_order_server.async.OrderOrderAopAsync;
import com.cn.dream_order_server.pojo.OrderOrder;
import com.cn.dream_order_server.pojo.UserSite;
import com.cn.dream_order_server.util.JsonUtils;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


@Aspect
@Component
public class OrderOrderAop {

    @Autowired
    private  StringRedisTemplate stringRedisTemplate;


    @Autowired
    private OrderOrderAopAsync orderOrderAopAsync;

    @AfterReturning(pointcut = "execution(* com.cn.dream_order_server.service.OrderOrderService.getOrderOrderListByUserId(..))",returning = "result")
    public void updateUserSitePhone(JoinPoint joinPoint , Object result){



        Object[] args = joinPoint.getArgs();
        PageInfo pageInfo = (PageInfo<OrderOrder>)result;

            for (Object object:pageInfo.getList()) {
                if (object instanceof OrderOrder){
                    OrderOrder orderOrder = (OrderOrder)object;
                    UserSite userSite = orderOrder.getUserSite();
                    String phone = userSite.getSitePhone();
                    String phoneNumber = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
                    userSite.setSitePhone(phoneNumber);
                    orderOrder.setUserSite(userSite);
                }
            }
            String objToString = JsonUtils.objToString(pageInfo);
            stringRedisTemplate.opsForValue().set("orderOrder-" + args[0] + "-"+ args[1]+"-"+args[2],objToString,30,TimeUnit.MINUTES);
            //执行异步操作
            orderOrderAopAsync.orderOrderAopupdateUserSitePhoneAsync((String) args[0],(Integer)args[1],(Integer)args[2]);



    }


    @AfterReturning(pointcut = "execution(* com.cn.dream_order_server.service.OrderOrderService.getOrderOrderListByUserIdAndOrdertypeId(..))",returning = "result")
    public void updateUserSitePhone1(JoinPoint joinPoint , Object result){

        Object[] args = joinPoint.getArgs();


        PageInfo pageInfo = (PageInfo<OrderOrder>)result;

            for (Object object:pageInfo.getList()) {
                if (object instanceof OrderOrder){
                    OrderOrder orderOrder = (OrderOrder)object;
                    UserSite userSite = orderOrder.getUserSite();
                    String phone = userSite.getSitePhone();
                    String phoneNumber = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
                    userSite.setSitePhone(phoneNumber);
                    orderOrder.setUserSite(userSite);
                }
            }
            String objToString = JsonUtils.objToString(pageInfo);

        orderOrderAopAsync.orderOrderAopupdateUserSitePhone1Async((String) args[0],(String)args[1],(Integer)args[2],(Integer)args[3],objToString);


    }




}
